﻿Imports System.Linq
Public Class Param

    Dim oLocalContext As DBMainContext


    Public Sub New()

        ' Cet appel est requis par le concepteur.
        InitializeComponent()

        ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().

    End Sub

    Public Sub Initialize(Optional ByVal oContext As DBMainContext = Nothing)
        oLocalContext = oContext

        If oLocalContext Is Nothing Then
            oLocalContext = New DBMainContext
        End If

        Dim oParamList As List(Of Parametre) = (From u In oLocalContext.Parametre Select u).ToList


        ParamDataGrid.DataSource = oParamList

        ParamDataGrid.Columns("ID").Visible = False

        With ParamDataGrid.Columns("Nom_Param")
            .HeaderText = "Nom"
            .ReadOnly = True
        End With

        With ParamDataGrid.Columns("Valeur_Param")
            .HeaderText = "Valeur"
        End With

        With ParamDataGrid.Columns("date_modif")
            .HeaderText = "Dernière Modifications"
            .ReadOnly = True
        End With

        LoadParam()

    End Sub

    Private Sub SaveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBtn.Click
        Dim listOChange1 As New List(Of Parametre)
        Dim listOChange2 As New List(Of Parametre_Couleur)


        listOChange1 = (From u In oLocalContext.GetChangeSet.Updates.OfType(Of Parametre)() Select u).ToList

        Dim oObject As Parametre_Couleur

        oObject = (From u In oLocalContext.Parametre_Couleur Where u.Nom_Param = "CouleurActivite" Select u).FirstOrDefault
        oObject.Valeur_CouleurHex = txtColorActivite.Text


        oObject = (From u In oLocalContext.Parametre_Couleur Where u.Nom_Param = "CouleurAccompagnement" Select u).FirstOrDefault
        oObject.Valeur_CouleurHex = txtColorAccomp.Text


        oObject = (From u In oLocalContext.Parametre_Couleur Where u.Nom_Param = "CouleurTache" Select u).FirstOrDefault
        oObject.Valeur_CouleurHex = txtColorTache.Text

        oObject = (From u In oLocalContext.Parametre_Couleur Where u.Nom_Param = "CouleurDispo" Select u).FirstOrDefault
        oObject.Valeur_CouleurHex = txtColorDispo.Text

        oObject = (From u In oLocalContext.Parametre_Couleur Where u.Nom_Param = "CouleurNonDispo" Select u).FirstOrDefault
        oObject.Valeur_CouleurHex = txtColorNDispo.Text




        For Each opara In listOChange1
            opara.date_Modif = Date.Now
        Next


        oLocalContext.SubmitChanges()

        Dim oParamList As List(Of Parametre) = (From u In oLocalContext.Parametre Select u).ToList


        ParamDataGrid.DataSource = oParamList

    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnColorActivite.Click
        ColorChooser.Color = btnColorActivite.BackColor
        ColorChooser.ShowDialog()
        txtColorActivite.Text = GetHexColor(ColorChooser.Color)
        btnColorActivite.BackColor = ColorChooser.Color
    End Sub

    Private Sub CancelBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CancelBtn.Click


        oLocalContext = New DBMainContext

        Dim oParamList As List(Of Parametre) = (From u In oLocalContext.Parametre Select u).ToList


        Dim ocust As New CustomMessageBox
        Dim oAjout As New AjoutEvenement(PlanificationEvenement.TypeEvenement.Tache)

        ocust.Height = oAjout.Height + 90
        ocust.Width = oAjout.Width
        ocust.Controls.Add(oAjout)

        ocust.ShowDialog()

        MsgBox(oAjout.rbTOUSLESJOURS.Checked)



        ParamDataGrid.DataSource = oParamList

    End Sub
    Public Function GetHexColor(ByVal colorObj As System.Drawing.Color) As String
        Return "#" & IIf(Hex(colorObj.R) = "", "00", Hex(colorObj.R)) & IIf(Hex(colorObj.G) = "", "00", Hex(colorObj.G)) & IIf(Hex(colorObj.B) = "", "00", Hex(colorObj.B))
    End Function
    Public Function GetColorHex(ByVal strHex As String) As Color
        Return ColorTranslator.FromHtml(strHex.PadRight(7, "0"))
    End Function


    Private Sub txtColorActivite_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtColorActivite.Validated
        Try
            btnColorActivite.BackColor = GetColorHex(txtColorActivite.Text)
        Catch ex As Exception
            Return
        End Try

    End Sub

    Private Sub SetColor_OnValidated(ByVal Button As Button, ByVal txtBox As TextBox)
        Try
            Button.BackColor = GetColorHex(txtBox.Text)
        Catch ex As Exception
            Return
        End Try

    End Sub



    Public Sub LoadParam()
        Dim oList As List(Of Parametre_Couleur)

        oList = (From u In oLocalContext.Parametre_Couleur Select u).ToList

        For Each Item In oList

            Select Case Item.Nom_Param
                Case "CouleurActivite"
                    txtColorActivite.Text = Item.Valeur_CouleurHex
                    SetColor_OnValidated(btnColorActivite, txtColorActivite)
                Case "CouleurAccompagnement"
                    txtColorAccomp.Text = Item.Valeur_CouleurHex
                    SetColor_OnValidated(btnColorAccomp, txtColorAccomp)
                Case "CouleurTache"
                    txtColorTache.Text = Item.Valeur_CouleurHex
                    SetColor_OnValidated(btnColorTache, txtColorTache)
                Case "CouleurDispo"
                    txtColorDispo.Text = Item.Valeur_CouleurHex
                    SetColor_OnValidated(btnColorDispo, txtColorDispo)
                Case "CouleurNonDispo"
                    txtColorNDispo.Text = Item.Valeur_CouleurHex
                    SetColor_OnValidated(btnColorNDispo, txtColorNDispo)
            End Select

        Next


    End Sub



    Private Sub txtColorAccomp_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtColorAccomp.Validated
        SetColor_OnValidated(btnColorAccomp, txtColorAccomp)
    End Sub

    Private Sub txtColorTache_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtColorTache.Validated
        SetColor_OnValidated(btnColorTache, txtColorTache)
    End Sub

    Private Sub txtColorDispo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtColorDispo.TextChanged
        SetColor_OnValidated(btnColorDispo, txtColorDispo)
    End Sub

    Private Sub txtColorNDispo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtColorNDispo.TextChanged
        SetColor_OnValidated(btnColorNDispo, txtColorNDispo)
    End Sub

    Private Sub btnColorAccomp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnColorAccomp.Click
        ColorChooser.Color = btnColorAccomp.BackColor
        ColorChooser.ShowDialog()
        txtColorAccomp.Text = GetHexColor(ColorChooser.Color)
        btnColorAccomp.BackColor = ColorChooser.Color
    End Sub

    Private Sub btnColorTache_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnColorTache.Click
        ColorChooser.Color = btnColorTache.BackColor
        ColorChooser.ShowDialog()
        txtColorTache.Text = GetHexColor(ColorChooser.Color)
        btnColorTache.BackColor = ColorChooser.Color
    End Sub

    Private Sub btnColorDispo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnColorDispo.Click
        ColorChooser.Color = btnColorDispo.BackColor
        ColorChooser.ShowDialog()
        txtColorDispo.Text = GetHexColor(ColorChooser.Color)
        btnColorDispo.BackColor = ColorChooser.Color
    End Sub

    Private Sub btnCouleurNDispo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnColorNDispo.Click
        ColorChooser.Color = btnColorNDispo.BackColor
        ColorChooser.ShowDialog()
        txtColorNDispo.Text = GetHexColor(ColorChooser.Color)
        btnColorNDispo.BackColor = ColorChooser.Color
    End Sub

    Private Sub Param_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class
